<?php
/**
 * 显示文章
 * 
 * ============================================================================
 * 版权所有: (c) 2009-2011，<UEShop Inc>，并保留所有权利。
 * 网站地址: http://www.ueshop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 *
 * @author      cmpan
 * @copyright   Copyright (c) 2009-2010 UEShop Inc. (http://www.ueshop.cn)
 * @license     http://www.ueshop.cn/1_0.txt
 */
class ShowController extends UE_Controller_Action {
	
	public function indexAction() {
		$cats = $this->tpl->cats;
		// 根据序号排序
		$cat_homepage_orders = array();		
		foreach ($cats as $key => $row) {
			$cat_homepage_orders[$key] = $row['cat_homepage_order'];
		}
		
		array_multisort($cat_homepage_orders, SORT_ASC, $cats);
		
		$catsList = array();
		foreach ($cats as $val) {
			$catsList[$val['cat_id']] = $val;
		}
		
		$this->tpl->assign('title', OptionModel::getOption('SITE_NAME'). '首页');
		$this->tpl->assign('catsList', $catsList);
		$this->tpl->apply('info/show.index');
	}
	
	public function listAction() {
	    if (empty($this->id)) {
	    	header('Location:'.UE::context());
	    	return ;
	    }	    
	    
	    if ($this->id == 'unclassified') {	    	
		    $cat = $this->tpl->cats[0];
	    } else {
	    	$cat = $this->tpl->cats[$this->id];
	    }
	    
	    $list = array();
	    
	    if ($cat['hasChile']) {
	    	$tpl = $cat['cat_tpl'];
	    } else {
	    	$tpl = $cat['cat_list_tpl'];
	    }
	    
    	$totals = UE_DB::factory()->getfirstField("SELECT COUNT(*) FROM ue_info WHERE cat_id IN({$cat['chileId']}) AND info_status > 0");
    	if ($totals) {	    		
	    	$paging = new UE_Paging();
	    	$paging->setVar($totals, 15);
	    	
	    	$sql = "SELECT * FROM ue_info 
	    	        WHERE cat_id IN({$cat['chileId']})
	    	          AND info_status > 0
	    	        ORDER BY info_order DESC, info_id DESC
	    	        LIMIT {$paging->firstCount}, {$paging->pageNo}";
	    	$list = UE_DB::factory()->getAll($sql);
	    	$this->tpl->assign('paging', $paging->pagingStr);
    	}
	    
	    
	    //print_r($this->tpl->cats);
	    //print_r($list);
	    //UE_DB::factory()->query('ALTER TABLE `ue_info` ADD `item_url` VARCHAR( 200 ) NOT NULL DEFAULT \'\'');
	    //getInfoList(1);
	    
		//$catsList = $this->tpl->getVar('cats');
		$catsList = $this->tpl->cats;
		
		// 根据序号排序
		$cat_homepage_orders = array();		
		foreach ($catsList as $key => $row) {
			$cat_homepage_orders[$key] = $row['cat_homepage_order'];
		}		
		array_multisort($cat_homepage_orders, SORT_ASC, $catsList);
		$this->tpl->assign('catsList', $catsList);
		
	    // 获取面包屑导航
	    $crumb = array();
	    getCrumb($this->id, $crumb);	    	    
	    $this->tpl->assign('crumb', $crumb);
	    $this->tpl->assign('list', $list);
	    $this->tpl->assign('title', $cat['cat_name']);
	    //$this->tpl->apply($tpl);
	    $this->tpl->apply('info/show.list');
	}
	
	public function itemAction() {
	    if (empty($this->id)) {
	    	print "Error Param!";
	    	return ;
	    }
	    $db = UE_DB::factory();
	    $sql = "SELECT * FROM ue_info WHERE info_id='{$this->id}' LIMIT 1";
	    $item = $db->getOne($sql);
	    
	    if ($item) {
	    	$sql = "SELECT info_content FROM ue_info_content WHERE info_id='{$this->id}' LIMIT 1";
	    	$item['info_content'] = $db->getfirstField($sql);
	    	// TODO 防刷新
	    	$db->query("UPDATE ue_info SET info_views=info_views+1 WHERE info_id='{$this->id}' LIMIT 1");
	    }
	    
	    $itemCat = array();
		$cats = $this->tpl->getVar('cats');
		$itemCat = $cats[$item['cat_id']];
	    
	    // 获取面包屑导航
	    $crumb = array();
	    getCrumb($item['cat_id'], $crumb);
	    	    
	    $this->tpl->assign('crumb', $crumb);
	    $this->tpl->assign('title', $item['info_title']);
	    $this->tpl->assign('item', $item);
	    $this->tpl->assign('itemCat', $itemCat);
	    $this->tpl->apply('info/show.item');
	}
	
	public function feedAction() {
		$this->id = (int)$this->id;
		$wh = '';
		if ($this->id) {
			$cats = CatModel::getCats();
			if (!empty($cats[$this->id])) {
				$wh = " AND cat_id IN({$cats[$this->id]['chileId']}) ";
			}
			
		}
		
		$sql = "SELECT * FROM ue_info 
		        WHERE true $wh
    	          AND info_status > 0
    	        ORDER BY info_id DESC
    	        LIMIT 20";
    	$rs = UE_DB::factory()->getAll($sql);
    	
    	$feeds = array();
    	if ($rs) {
    		foreach ($rs as $r) {
    			$feed['feed_title'] = $r['info_title'];
    			$feed['feed_link'] = (UE_Config::get('full_url') ? '' : UE::site().UE::context()) . $r['item_url'];
    			$feed['feed_author'] = $r['info_author'];
    			$feed['feed_pub_date'] = date('M, d D Y H:i:s +0000', $r['info_modified_time']);
    			$feed['feed_desc'] = $r['info_desc'];
    			
    			$feeds[] = $feed;
    		}
    	}
    	
    	header('Content-Type: application/xml');
    	$this->tpl->assign('feeds', $feeds);
    	$this->tpl->apply('feed');
	}
}